/*
 * Ensembl DSN Parser
 * Returns a "result" object which contain two attributes. "allDsnEnsembl"
 * is an array with all the Dsn of Ensembl and "dsnArrayPosition" holds the
 * position (integer) for the "dsnId" in the "allDsnEnsembl" array.
 */

YAHOO.dasBrowser.genome.karyotypeStructure.dsn.parseEnsemblDsn = function(responseXML, dsnId){
    var result = {
        allEnsemblDsn: new Array(),
		referenceLocation: null,
		karyotypeLocation: null
    };
	sId = YAHOO.dasBrowser.genome.karyotypeStructure.dsn.splitEnsemblDsnSourceId(dsnId);
	dsnKaryotype = sId.sourceSpecie + "." + sId.sourceRelease + ".karyotype";	
	
	if (responseXML.documentElement != null) {
		var DSNNodes = responseXML.documentElement.getElementsByTagName('DSN');
		for (var j = 0; j < DSNNodes.length; j++) {
			if (DSNNodes[j].nodeName == 'DSN') {
				var ensemblDsn = new myEnsemblDsn();
				var dsnAttrs = DSNNodes[j].attributes;
				for (var i = dsnAttrs.length - 1; i >= 0; i--) {
					if (dsnAttrs[i].name == 'href') {
						ensemblDsn.setDsnUrl(dsnAttrs[i].value);
					}
				}
				if (DSNNodes[j].hasChildNodes()) {
					var DSNNodesChildNodes = DSNNodes[j].childNodes;
					DSNNodesChildNodes_loop: for (var i = 0; i < DSNNodesChildNodes.length; i++) {
						if (DSNNodesChildNodes[i].nodeName == 'DESCRIPTION') {
							ensemblDsn.setDescription(DSNNodesChildNodes[i].firstChild.nodeValue);
						}
						else 
							if (DSNNodesChildNodes[i].nodeName == 'MAPMASTER') {
								ensemblDsn.setMapmaster(DSNNodesChildNodes[i].firstChild.nodeValue);
							}
							else 
								if (DSNNodesChildNodes[i].nodeName == 'SOURCE') {
									var sourceAttrs = DSNNodesChildNodes[i].attributes;
									for (var n = sourceAttrs.length - 1; n >= 0; n--) {
										if (sourceAttrs[n].name == 'id') {
											ensemblDsn.setSourceId(sourceAttrs[n].value);
											if (sourceAttrs[n].value == dsnId) {
												result.referenceLocation = result.allEnsemblDsn.length;
											}
											else 
												if (sourceAttrs[n].value == dsnKaryotype) {
													result.karyotypeLocation = result.allEnsemblDsn.length;
												}
										}
									}
									
								}
					}
				}
				result.allEnsemblDsn.push(ensemblDsn);
			}
		}
	}
    return result
}



/*
 * DSN Parser
 */

YAHOO.dasBrowser.genome.karyotypeStructure.dsn.parseDsn = function(responseXML){
    var result = {
        allDsn: new Array()
    };

	if (responseXML.documentElement != null) {
		var DSNNodes = responseXML.documentElement.getElementsByTagName('DSN');
		for (var j = 0; j < DSNNodes.length; j++) {
			if (DSNNodes[j].nodeName == 'DSN') {
				var _dsn = new myDsn();
				var dsnAttrs = DSNNodes[j].attributes;
				for (var i = dsnAttrs.length - 1; i >= 0; i--) {
					if (dsnAttrs[i].name == 'href') {
						_dsn.setDsnUrl(dsnAttrs[i].value);
						
					}
				}
				if (DSNNodes[j].hasChildNodes()) {
					var DSNNodesChildNodes = DSNNodes[j].childNodes;
					DSNNodesChildNodes_loop: for (var i = 0; i < DSNNodesChildNodes.length; i++) {
						if (DSNNodesChildNodes[i].nodeName == 'DESCRIPTION') {
							_dsn.setDescription(DSNNodesChildNodes[i].firstChild.nodeValue);
						}
						else 
							if (DSNNodesChildNodes[i].nodeName == 'MAPMASTER') {
								_dsn.setMapmaster(DSNNodesChildNodes[i].firstChild.nodeValue);
							}
							else 
								if (DSNNodesChildNodes[i].nodeName == 'SOURCE') {
									var sourceAttrs = DSNNodesChildNodes[i].attributes;
									for (var n = sourceAttrs.length - 1; n >= 0; n--) {
										if (sourceAttrs[n].name == 'id') {
											_dsn.setSourceId(sourceAttrs[n].value);
										}
									}
									
								}
					}
				}
				result.allDsn.push(_dsn);
			}
		}
	}
    return result
}



/*
 * Source Parser
 */
// uri="uniprot2probes" doc_href="http://code.google.com/p/enfin-core" title="uniprot2probes" description="Annotation source with microarray probes mapping uniprot sequences"
YAHOO.dasBrowser.genome.karyotypeStructure.dsn.parseSource = function(responseXML){
    var result = {
        source: new Array()
    };
    
	if (responseXML.documentElement != null) {
		var sourceNodes = responseXML.documentElement.getElementsByTagName('SOURCE');
		for (var j = 0; j < sourceNodes.length; j++) {
			if (sourceNodes[j].nodeName == 'SOURCE') {
				var _source = new mySource();
				var sourceAttrs = sourceNodes[j].attributes;
				for (var i = sourceAttrs.length - 1; i >= 0; i--) {
					if (sourceAttrs[i].name == 'uri') {
						_source.setUri(sourceAttrs[i].value);
					} else if(sourceAttrs[i].name == 'title') {
						_source.setTitle(sourceAttrs[i].value);
					} else if(sourceAttrs[i].name == 'description') {
						_source.setDescription(sourceAttrs[i].value);
					}
				}
				result.source.push(_source);
			}
		}
	}
    return result
}
/*
 * Ensembl Entry Points Parser
 */
YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.parseEnsemblEntryPoints = function(responseXML, entryPointsType){
    var result = {
        allEnsemblEntryPoints: new Array(),
        ids: new Array()
    };
	if (responseXML.documentElement != null) {
		var segmentNodes = responseXML.documentElement.getElementsByTagName('SEGMENT');
		for (var j = 0; j < segmentNodes.length; j++) {
			if (segmentNodes[j].nodeName == 'SEGMENT') {
				var segmentId = new String;
				var segmentType = "";
				var chrId = new String;
				var ensemblEntryPoint = new myEnsemblEntryPoints();
				ensemblEntryPoint.setSegment(segmentNodes[j].firstChild.nodeValue);
				var segmentAttrs = segmentNodes[j].attributes;
				for (var i = segmentAttrs.length - 1; i >= 0; i--) {
					if (segmentAttrs[i].name == 'type') {
						ensemblEntryPoint.setType(segmentAttrs[i].value);
						segmentType = segmentAttrs[i].value;
					}
					else 
						if (segmentAttrs[i].name == 'id') {
							ensemblEntryPoint.setId(segmentAttrs[i].value);
							segmentId = segmentAttrs[i].value;
						}
						else 
							if (segmentAttrs[i].name == 'start') {
								ensemblEntryPoint.setStart(segmentAttrs[i].value);
							}
							else 
								if (segmentAttrs[i].name == 'stop') {
									ensemblEntryPoint.setStop(segmentAttrs[i].value);
								}
								else 
									if (segmentAttrs[i].name == 'orientation') {
										ensemblEntryPoint.setOrientation(segmentAttrs[i].value);
									}
									else 
										if (segmentAttrs[i].name == 'subparts') {
											ensemblEntryPoint.setSubparts(segmentAttrs[i].value);
										}
				}
				chrId = segmentType.toString() + segmentId;
				if (segmentType == entryPointsType) {
					result.ids.push(chrId);
				//Unknow segment type. It should not be added. However it is included to get chromosomes from NCBI36
				} else if(segmentType.length == 0){ 
				    chrId=entryPointsType+chrId
					result.ids.push(chrId);
				}
				result.allEnsemblEntryPoints[chrId] = ensemblEntryPoint;
			}
		}
	}
    return result
}

/*
 * Stylesheet Parser
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.stylesheet.parse = function(responseXML){
    var result = {
		allKaryotypeStylesheetTypes: new Array()
    };
    
	var category = new String();
	var karyotypeStylesheet = null;
	if (responseXML.documentElement != null) {
		var categoryNodes = responseXML.documentElement.getElementsByTagName('CATEGORY');
		for (var j = 0; j < categoryNodes.length; j++) {
			if (categoryNodes[j].nodeName == 'CATEGORY') {
				var categoryAttrs = categoryNodes[j].attributes;
				for (var i = categoryAttrs.length - 1; i >= 0; i--) {
					if (categoryAttrs[i].name == 'id') {
						category = categoryAttrs[i].value;
					}
				}
				if (categoryNodes[j].hasChildNodes()) {
					var categoryNodesChildNodes = categoryNodes[j].childNodes;
					categoryNodesChildNodes_loop: for (var p = 0; p < categoryNodesChildNodes.length; p++) {
						if (categoryNodesChildNodes[p].nodeName == 'TYPE') {
							karyotypeStylesheet = new myStylesheet();
							karyotypeStylesheet.setCategoryId(category);
							var typeAttrs = categoryNodesChildNodes[p].attributes;
							for (var i = typeAttrs.length - 1; i >= 0; i--) {
								if (typeAttrs[i].name == 'id') {
									karyotypeStylesheet.setTypeId(typeAttrs[i].value);
								}
							}
							if (categoryNodesChildNodes[p].hasChildNodes()) {
								var typeNodesChildNodes = categoryNodesChildNodes[p].childNodes;
								typeNodesChildNodes_loop: for (var h = 0; h < typeNodesChildNodes.length; h++) {
									if (typeNodesChildNodes[h].nodeName == 'GLYPH') {
										if (typeNodesChildNodes[h].hasChildNodes()) {
											var glyphNodesChildNodes = typeNodesChildNodes[h].childNodes;
											glyphNodesChildNodes_loop: for (var t = 0; t < glyphNodesChildNodes.length; t++) {
												//if (glyphNodesChildNodes[t].nodeName == 'BOX') {
												//'ARROW','ANCHORED_ARROW','BOX','CROSS','EX','HIDDEN','LINE','SPAN','TEXT','TOOMANY','TRIANGLE','PRIMERS'
												if (glyphNodesChildNodes[t].nodeName.length > 0) {
													if (glyphNodesChildNodes[t].hasChildNodes()) {
														var boxNodesChildNodes = glyphNodesChildNodes[t].childNodes;
														boxNodesChildNodes_loop: for (var e = 0; e < boxNodesChildNodes.length; e++) {
															if (boxNodesChildNodes[e].nodeName == 'HEIGHT') {
																karyotypeStylesheet.setHeight(boxNodesChildNodes[e].firstChild.nodeValue);
															}
															else 
																if (boxNodesChildNodes[e].nodeName == 'LABEL') {
																	karyotypeStylesheet.setLabel(boxNodesChildNodes[e].firstChild.nodeValue);
																}
																else 
																	if (boxNodesChildNodes[e].nodeName == 'BGCOLOR') {
																		karyotypeStylesheet.setBgcolor(boxNodesChildNodes[e].firstChild.nodeValue);
																	}
																	else 
																		if (boxNodesChildNodes[e].nodeName == 'FGCOLOR') {
																			karyotypeStylesheet.setFgcolor(boxNodesChildNodes[e].firstChild.nodeValue);
																		}
																		else 
																			if (boxNodesChildNodes[e].nodeName == 'BUMP') {
																				karyotypeStylesheet.setBump(boxNodesChildNodes[e].firstChild.nodeValue);
																			}
														}
													}
												}
											}
										}
									}
								}
							}
							result.allKaryotypeStylesheetTypes[karyotypeStylesheet.typeId] = karyotypeStylesheet;
						//result.allKaryotypeStylesheetTypes.push(karyotypeStylesheet);
						}
					}
				}
			}
		}
	}
    return result
}


/*
 * Annotation Features Parser
 */
YAHOO.dasBrowser.genome.annotationSources.parseAnnotationFeatures = function(responseXML){
    var result = {
        segment: null,
		allfeatures: new Array()
    };

    
	if (responseXML.documentElement != null) {

		var segmentNodes = responseXML.documentElement.getElementsByTagName('SEGMENT');
		for (var j = 0; j < segmentNodes.length; j++) {
			if (segmentNodes[j].nodeName == 'SEGMENT') {
				var annotationSegment = new myAnnotationSegment();
				var segmentAttrs = segmentNodes[j].attributes;
				for (var i = segmentAttrs.length - 1; i >= 0; i--) {
					if (segmentAttrs[i].name == 'id') {
						annotationSegment.setId(segmentAttrs[i].value);
					}
					else 
			        if (segmentAttrs[i].name == 'start') {
						annotationSegment.setStart(segmentAttrs[i].value);
					}
					else 
					if (segmentAttrs[i].name == 'stop') {
						annotationSegment.setStop(segmentAttrs[i].value);
					}
					else 
					if (segmentAttrs[i].name == 'type') { 
						annotationSegment.setType(segmentAttrs[i].value);
					}
					else 
					if (segmentAttrs[i].name == 'version') { 
						annotationSegment.setVersion(segmentAttrs[i].value);
					}
					else 
					if (segmentAttrs[i].name == 'label') {
						annotationSegment.setLabel(segmentAttrs[i].value);						
					}
				}
				if (segmentNodes[j].hasChildNodes()) {
					var segmentNodesChildNodes = segmentNodes[j].childNodes;
					segmentNodesChildNodes_loop: for (var p = 0; p < segmentNodesChildNodes.length; p++) {
						if (segmentNodesChildNodes[p].nodeName == 'FEATURE') {
							var annotationFeature = new myAnnotationFeature();
							var featureAttrs = segmentNodesChildNodes[p].attributes;
							for (var i = featureAttrs.length - 1; i >= 0; i--) {
								if (featureAttrs[i].name == 'id') {
									annotationFeature.setId(featureAttrs[i].value);
								}
								else 
									if (featureAttrs[i].name == 'label') { 
										annotationFeature.setLabel(featureAttrs[i].value);
									}
							}
							if (segmentNodesChildNodes[p].hasChildNodes()) {
								var featureNodesChildNodes = segmentNodesChildNodes[p].childNodes;
								featureNodesChildNodes_loop: for (var h = 0; h < featureNodesChildNodes.length; h++) {
									if (featureNodesChildNodes[h].nodeName == 'START') {
										if (featureNodesChildNodes[h].firstChild != null) {
											annotationFeature.setStart(featureNodesChildNodes[h].firstChild.nodeValue);
										}
										else {
											annotationFeature.setStart("");
										}
									}
									else 
										if (featureNodesChildNodes[h].nodeName == 'END') {
											if (featureNodesChildNodes[h].firstChild != null) {
												annotationFeature.setEnd(featureNodesChildNodes[h].firstChild.nodeValue);
											}
											else {
												annotationFeature.setEnd("");
											}
										}
									else 
									if (featureNodesChildNodes[h].nodeName == 'METHOD') {
										if (featureNodesChildNodes[h].firstChild != null) {
											annotationFeature.setMethodName(featureNodesChildNodes[h].firstChild.nodeValue);
											annotationSegment.addFeatureMethod(featureNodesChildNodes[h].firstChild.nodeValue);
										}
										else {
											annotationFeature.setMethodName("");
											annotationSegment.addFeatureMethod("");
										}
										var methodAttrs = featureNodesChildNodes[h].attributes;
										for (var i = methodAttrs.length - 1; i >= 0; i--) {
											if (methodAttrs[i].name == 'id') {
												annotationFeature.setMethodId(methodAttrs[i].value);
											}
										}
									}
									else 
									if (featureNodesChildNodes[h].nodeName == 'SCORE') {
										if (featureNodesChildNodes[h].firstChild != null) {
											var score = featureNodesChildNodes[h].firstChild.nodeValue;
											//var score = Math.random() * 100; // TESTING PURPOSES: delete this as soon as possible
											annotationFeature.setScore(score);
										}
										else {
											annotationFeature.setScore("");
										}
									}
									else 
									if (featureNodesChildNodes[h].nodeName == 'ORIENTATION') {
										if (featureNodesChildNodes[h].firstChild != null) {
											annotationFeature.setOrientation(featureNodesChildNodes[h].firstChild.nodeValue);
										}
										else {
											annotationFeature.setOrientation("");
										}
										
									}
									else 
									if (featureNodesChildNodes[h].nodeName == 'PHASE') { 
										if (featureNodesChildNodes[h].firstChild != null) {
											annotationFeature.setPhase(featureNodesChildNodes[h].firstChild.nodeValue);
										}
										else {
											annotationFeature.setPhase("");
										}
									}
									else 
									if (featureNodesChildNodes[h].nodeName == 'NOTE') { 
										if (featureNodesChildNodes[h].firstChild != null) {
											annotationFeature.setNote(featureNodesChildNodes[h].firstChild.nodeValue);
										}
										else {
											annotationFeature.setNote("");
										}
									}
									else 
									if (featureNodesChildNodes[h].nodeName == 'LINK') { 
										if (featureNodesChildNodes[h].firstChild != null) {
											annotationFeature.setLinkText(featureNodesChildNodes[h].firstChild.nodeValue);
										}
										else {
											annotationFeature.setLinkText("");
										}
										var linkAttrs = featureNodesChildNodes[h].attributes;
										for (var i = linkAttrs.length - 1; i >= 0; i--) {
											if (linkAttrs[i].name == 'url' || linkAttrs[i].name == 'href') { 
												annotationFeature.setLinkUrl(linkAttrs[i].value);
											}
										}
									}
									else 
									if (featureNodesChildNodes[h].nodeName == 'GROUP') {
										var groupAttrs = featureNodesChildNodes[h].attributes;
										for (var i = groupAttrs.length - 1; i >= 0; i--) {
											if (groupAttrs[i].name == 'id') { 
												annotationFeature.setGroupId(groupAttrs[i].value);
											}
											else 
											if (groupAttrs[i].name == 'label') { 
												annotationFeature.setGroupLabel(groupAttrs[i].value);
											}
											else 
											if (groupAttrs[i].name == 'type') { 
												annotationFeature.setGroupType(groupAttrs[i].value);
												
											}
										}
									}
									else 
										if (featureNodesChildNodes[h].nodeName == 'TARGET') { 
											if (featureNodesChildNodes[h].firstChild != null) {
												annotationFeature.setTargetName(featureNodesChildNodes[h].firstChild.nodeValue);
											}
											else {
												annotationFeature.setTargetName("");
											}
											var targetAttrs = featureNodesChildNodes[h].attributes;
											for (var i = targetAttrs.length - 1; i >= 0; i--) {
												if (targetAttrs[i].name == 'id') { 
													annotationFeature.setTargetId(targetAttrs[i].value);
												}
												else 
												if (targetAttrs[i].name == 'start') { 
													annotationFeature.setTargetStart(targetAttrs[i].value);
												}
												else 
												if (targetAttrs[i].name == 'stop') { 
													annotationFeature.setTargetStop(targetAttrs[i].value);
												}
											}
										}
										else 
											if (featureNodesChildNodes[h].nodeName == 'TYPE'){
												
												if (featureNodesChildNodes[h].firstChild != null) {
													annotationFeature.setTypeName(featureNodesChildNodes[h].firstChild.nodeValue);
													var featureTypeValue = featureNodesChildNodes[h].firstChild.nodeValue;
													annotationFeature.setTypeName(featureTypeValue);
													annotationSegment.addFeatureType(featureTypeValue);
													
												}
												else {
													annotationFeature.setTypeName("");
													annotationSegment.addFeatureType("");
												}
												var typeAttrs = featureNodesChildNodes[h].attributes;
												for (var i = typeAttrs.length - 1; i >= 0; i--) {
													if (typeAttrs[i].name == 'id') {
														annotationFeature.setTypeId(typeAttrs[i].value);
													}
													else 
													if (typeAttrs[i].name == 'category') {
														annotationFeature.setTypeCategory(typeAttrs[i].value);
														annotationSegment.addFeatureCategory(typeAttrs[i].value);
													}
													else 
													if (typeAttrs[i].name == 'reference') { 
														annotationFeature.setTypeReference(typeAttrs[i].value);
													}
												}
												if (annotationFeature.getTypeCategory() == "") {
													annotationSegment.addFeatureCategory("");
												}
											}
								}
								if (annotationFeature.getStart() == 0 || annotationFeature.getEnd() == 0) {
									annotationSegment.setNonPositional();
									if (annotationFeature.getId() == "feature_number") {
										if (annotationFeature.tempScore != null) {
											annotationSegment.setFeatureNumber(annotationFeature.tempScore);
										}
									}
								}
								else {
									annotationSegment.setPositional();
									annotationSegment.addScore(annotationFeature.getScore());
								}
							}
							result.allfeatures.push(annotationFeature);
						}
					}
				}
				//console.log("FN: "+annotationSegment.featureNumber);
				//console.log("FND: "+annotationSegment.featureNumberDisplayed);
				result.segment = annotationSegment;
			}
		}
	}
    return result
}

/*
 * Ensembl sequence parser
 */

YAHOO.dasBrowser.genome.karyotypeStructure.sequence.parse = function(responseXML){
    var result = new Object();
	result.dnaSequence = '';
	if (responseXML.documentElement != null) {
		var sequenceNodes = responseXML.documentElement.getElementsByTagName('SEQUENCE');
		for (var j = 0; j < sequenceNodes.length; j++) {
			if (sequenceNodes[j].nodeName == 'SEQUENCE') {
				//var annotationSegment = new myAnnotationSegment();
				var sequenceAttrs = sequenceNodes[j].attributes;
				for (var i = sequenceAttrs.length - 1; i >= 0; i--) {
					if (sequenceAttrs[i].name == 'id') {
						result.sequenceId = sequenceAttrs[i].value;
					}
					else 
						if (sequenceAttrs[i].name == 'start') {
							result.sequenceStart = sequenceAttrs[i].value;
						}
						else 
							if (sequenceAttrs[i].name == 'stop') {
								result.sequenceStop = sequenceAttrs[i].value;
							}
							else 
								if (sequenceAttrs[i].name == 'version') {
									result.sequenceVersion = sequenceAttrs[i].value;
								}
				}
				if (sequenceNodes[j].hasChildNodes()) {
					var sequenceNodesChildNodes = sequenceNodes[j].childNodes;
					sequenceNodesChildNodes_loop: for (var p = 0; p < sequenceNodesChildNodes.length; p++) {
						if (sequenceNodesChildNodes[p].nodeName == 'DNA') {
							//var annotationFeature = new myAnnotationFeature();
							var dnaAttrs = sequenceNodesChildNodes[p].attributes;
							for (var i = dnaAttrs.length - 1; i >= 0; i--) {
								if (dnaAttrs[i].name == 'length') {
									result.dnaLength = dnaAttrs[i].value;
								}
							}
							if (sequenceNodesChildNodes[p].firstChild != null) {
								var dnaSequence = sequenceNodesChildNodes[p].firstChild.nodeValue;
								result.dnaSequence = dnaSequence.replace(/\n/g, "");
							}
						}
					}
				}
				if (result.dnaSequence == '') {
					if (sequenceNodes[j].firstChild != null) {
						var dnaSequence = sequenceNodes[j].firstChild.nodeValue;
						result.dnaSequence = dnaSequence.replace(/\n/g, "");
					}
				}
			}
		}
	}
	return result
}




/*
 * DAS registry parser
 */
YAHOO.dasBrowser.dasRegistry.parse = function(responseXML){
    var result = {
		sources: new Array()
    };
	if (responseXML.documentElement != null) {
		var sourceNodes = responseXML.documentElement.getElementsByTagName('SOURCE');
		for (var j = 0; j < sourceNodes.length; j++) {
			if (sourceNodes[j].nodeName == 'SOURCE') {
				var sourceObject = new myDasSource();
				var sourceAttrs = sourceNodes[j].attributes;
				for (var i = sourceAttrs.length - 1; i >= 0; i--) {
					if (sourceAttrs[i].name == 'uri') {
						sourceObject.setUri(sourceAttrs[i].value);
						
					}
					else 
						if (sourceAttrs[i].name == 'doc_href') {
							sourceObject.setDocHref(sourceAttrs[i].value);
						}
						else 
							if (sourceAttrs[i].name == 'description') {
								sourceObject.setDescription(sourceAttrs[i].value);
							}
							else 
								if (sourceAttrs[i].name == 'title') {
									sourceObject.setTitle(sourceAttrs[i].value);
								}
				}
				if (sourceNodes[j].hasChildNodes()) {
					var sourceNodesChildNodes = sourceNodes[j].childNodes;
					sourceNodesChildNodes_loop: for (var h = 0; h < sourceNodesChildNodes.length; h++) {
						if (sourceNodesChildNodes[h].nodeName == 'MAINTAINER') {
							var maintainerAttrs = sourceNodesChildNodes[h].attributes;
							for (var i = maintainerAttrs.length - 1; i >= 0; i--) {
								if (maintainerAttrs[i].name == 'email') {
									sourceObject.setMaintainerEmail(maintainerAttrs[i].value);
								}
							}
						}
						else 
							if (sourceNodesChildNodes[h].nodeName == 'VERSION') {
								var versionAttrs = sourceNodesChildNodes[h].attributes;
								for (var i = versionAttrs.length - 1; i >= 0; i--) {
									if (versionAttrs[i].name == 'uri') {
										sourceObject.setVersionUri(versionAttrs[i].value);
									}
									else 
										if (versionAttrs[i].name == 'created') {
											sourceObject.setVersionCreated(versionAttrs[i].value);
										}
								}
								if (sourceNodesChildNodes[h].hasChildNodes()) {
									var versionNodesChildNodes = sourceNodesChildNodes[h].childNodes;
									versionNodesChildNodes_loop: for (var p = 0; p < versionNodesChildNodes.length; p++) {
										if (versionNodesChildNodes[p].nodeName == 'COORDINATES') {
											var dasSourceCoordinatesObject = new myDasSourceCoordinates();
											if (versionNodesChildNodes[p].firstChild != null) {
												dasSourceCoordinatesObject.setName(versionNodesChildNodes[p].firstChild.nodeValue);
											}
											else {
												dasSourceCoordinatesObject.setName("");
											}
											var coordinatesAttrs = versionNodesChildNodes[p].attributes;
											for (var i = coordinatesAttrs.length - 1; i >= 0; i--) {
												if (coordinatesAttrs[i].name == 'uri') {
													dasSourceCoordinatesObject.setUri(coordinatesAttrs[i].value);
												}
												else 
													if (coordinatesAttrs[i].name == 'source') {
														dasSourceCoordinatesObject.setSource(coordinatesAttrs[i].value);
													}
													else 
														if (coordinatesAttrs[i].name == 'authority') {
															dasSourceCoordinatesObject.setAuthority(coordinatesAttrs[i].value);
														}
														else 
															if (coordinatesAttrs[i].name == 'test_range') {
																dasSourceCoordinatesObject.setTestRange(coordinatesAttrs[i].value);
															}
											}
											sourceObject.addCoordinates(dasSourceCoordinatesObject);
										}
										
										
										if (versionNodesChildNodes[p].nodeName == 'CAPABILITY') {
											var dasSourceCapabilityObject = new myDasSourceCapability();
											var capabilityAttrs = versionNodesChildNodes[p].attributes;
											for (var i = capabilityAttrs.length - 1; i >= 0; i--) {
												if (capabilityAttrs[i].name == 'type') {
													dasSourceCapabilityObject.setType(capabilityAttrs[i].value);
												}
												else 
													if (capabilityAttrs[i].name == 'query_uri') {
														dasSourceCapabilityObject.setQueryUri(capabilityAttrs[i].value);
													}
											}
											sourceObject.addCapability(dasSourceCapabilityObject);
										}
										
										
										if (versionNodesChildNodes[p].nodeName == 'PROP') {
											//console.log("PROP");
											var dasSourcePropObject = new myDasSourceProp();
											var propAttrs = versionNodesChildNodes[p].attributes;
											for (var i = propAttrs.length - 1; i >= 0; i--) {
												if (propAttrs[i].name == 'name') {
													dasSourcePropObject.setName(propAttrs[i].value);
												//console.log(propAttrs[i].value);
												}
												else 
													if (propAttrs[i].name == 'value') {
														dasSourcePropObject.setValue(propAttrs[i].value);
													//console.log(propAttrs[i].value);
													}
											}
											if (dasSourcePropObject.name.toLowerCase() == 'label') {
												//console.log(dasSourcePropObject.value);
												sourceObject.addLabel(dasSourcePropObject.value);
											}
											else 
												if (dasSourcePropObject.name.toLowerCase() == 'leasetime') {
													sourceObject.setLeaseTime(dasSourcePropObject.value);
												}
												else 
													if (dasSourcePropObject.name.toLowerCase() == 'projecthome') {
														sourceObject.setProjectHome(dasSourcePropObject.value);
													}
													else 
														if (dasSourcePropObject.name.toLowerCase() == 'projecticon') {
															sourceObject.setProjectIcon(dasSourcePropObject.value);
														}
														else 
															if (dasSourcePropObject.name.toLowerCase() == 'projectdesc') {
																sourceObject.setProjectDesc(dasSourcePropObject.value);
															}
															else 
																if (dasSourcePropObject.name.toLowerCase() == 'projectname') {
																	sourceObject.setProjectName(dasSourcePropObject.value);
																}
										}
									}
								}
							}
					}
				}
				result.sources.push(sourceObject);
			//result.sources[sourceObject.uri] = sourceObject;
			}
		}
	}
	return result;
}















